﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HousingInfo.Library;

namespace WebHousingInfo.Controls
{
    public partial class TimKiemTrangCon2 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getCategoryHouse();
                getHouseType();
                getAreas();
            }
        }

        private void getCategoryHouse()
        {
            try
            {
                var categoryHouse = CategoryInfo.GetCategoryInfo_BySingle(true, false, false, false, true);
                dlcategory.DataSource = categoryHouse;
                dlcategory.DataBind();
                dlcategory.Items.Insert(0, "Tất cả");
                dlcategory.SelectedIndex = 0;
            }
            catch { }
        }

        private void getHouseType()
        {
            try
            {
                var housingType = HousingTypeInfo.GetHousingTypeInfo();
                dlhousetype.DataSource = housingType;
                dlhousetype.DataBind();
                dlhousetype.Items.Insert(0, "Tất cả");
                dlhousetype.SelectedIndex = 0;
            }
            catch { }
        }

        private void getAreas()
        {
            try
            {
                var areas = AreaInfo.GetAreaInfo();
                if (areas != null)
                {
                    dlareas.DataSource = areas;
                    dlareas.DataBind();
                    dlareas.Items.Insert(0, "Tất cả");
                    dlareas.SelectedIndex = 0;
                    //lay danh sach khu vuc con
                    var areaId = dlareas.SelectedValue;
                    getAreaDetailWithAreaId(areaId);
                }
            }
            catch { }
        }
        private void getAreaDetailWithAreaId(string areaId)
        {
            try
            {
                if (areaId != "Tất cả")
                {
                    var areadetailList = AreaDetailInfo.GetAreaDetailInfo(int.Parse(areaId));
                    dlareasdetail.DataSource = areadetailList;
                    dlareasdetail.DataBind();
                    dlareasdetail.Items.Insert(0, "Tất cả");
                    dlareasdetail.SelectedIndex = 0;
                }
                else
                {
                    dlareasdetail.Items.Clear();
                    dlareasdetail.Items.Add("Tất cả");
                }
            }
            catch
            {
            }
        }

        protected void dlareas_SelectedIndexChanged(object sender, EventArgs e)
        {
            var areaId = dlareas.SelectedValue;
            getAreaDetailWithAreaId(areaId);
        }

        protected void imageSearch_Click(object sender, ImageClickEventArgs e)
        {
            string listparam = null;
            if (dlcategory.SelectedValue == "Tất cả")
                listparam += "-1,";
            else listparam += dlcategory.SelectedValue + ",";

            if (dlhousetype.SelectedValue == "Tất cả")
                listparam += "-1,";
            else listparam += dlhousetype.SelectedValue + ",";
            if (dlareas.SelectedValue == "Tất cả")
                listparam += "-1,";
            else listparam += dlareas.SelectedValue + ",";
            if (dlareasdetail.SelectedValue == "Tất cả")
                listparam += "-1";
            else listparam += dlareasdetail.SelectedValue;
            Session["SeachControl"] = listparam;
            Response.Redirect("Ketquatimkiem.aspx");
        }

    }
}